﻿#region Using Directives

using System.ComponentModel;
using System.IO;
using EnvDTE;
using FeatureBuilder.ContribLib.Helpers;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;

#endregion

namespace FeatureBuilder.ContribLib.Commands
{
    /// <summary>
    /// Feature Extension Command that adds a ProjectItem from a file.
    /// </summary>
    [DisplayName("Add ProjectItem from file")]
    [Description("Adds a ProjectItem from a file.")]
    [Category("IDE")]
    public class AddProjectItemFromFile : FeatureCommand    
    {
        public string SourceFilePath { get; set; }
        public Project TargetProject { get; set; }
        public bool Open { get; set; }
        public string ItemName { get; set; }
        public ProjectItem ProjectItem { get; set; }
        public bool OverwriteItemIfExists { get; set; }

        ProjectItem outputProjectItem = default(ProjectItem);

        #region Overrides

        public override void Execute()
        {
            try
            {
                if (!string.IsNullOrEmpty(ItemName))
                {
                    ProjectItem item = DteHelper.FindItemByName(TargetProject.ProjectItems, ItemName, true);

                    if (item != null)
                    {
                        AddItem(item, item.ProjectItems);
                    }
                }
                else if (ProjectItem != null)
                {
                    AddItem(ProjectItem, ProjectItem.ProjectItems);
                }
                else if (TargetProject != null)
                {
                    AddItem(TargetProject, TargetProject.ProjectItems);
                }

                if (Open && outputProjectItem != null)
                {
                    Window wnd = outputProjectItem.Open(Constants.vsViewKindPrimary);
                    wnd.Visible = true;
                    wnd.Activate();
                }
            }
            catch
            {
                if (outputProjectItem != null)
                {
                    outputProjectItem.Delete();
                }
            }

        }

        #endregion

        #region Privates

        /// <summary>
        /// Adds the item.
        /// </summary>
        /// <param name="projectItem">The project item.</param>
        /// <param name="projectItems">The project items.</param>
        private void AddItem(ProjectItem projectItem, ProjectItems projectItems)
        {
            if (!OverwriteItemIfExists)
            {
                outputProjectItem = DteHelper.FindItemByName(projectItems, Path.GetFileName(SourceFilePath), false);
                if (outputProjectItem == null)
                {
                    outputProjectItem = projectItems.AddFromFileCopy(SourceFilePath);
                }
            }
            else
            {
                File.Copy(SourceFilePath, Path.Combine(projectItem.Properties.Item("FullPath").Value.ToString(), Path.GetFileName(SourceFilePath)), true);
                outputProjectItem = projectItems.AddFromFile(Path.Combine(projectItem.Properties.Item("FullPath").Value.ToString(), Path.GetFileName(SourceFilePath)));
            }
        }

        /// <summary>
        /// Adds the item.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="projectItems">The project items.</param>
        private void AddItem(Project project, ProjectItems projectItems)
        {
            if (!OverwriteItemIfExists)
            {
                outputProjectItem = DteHelper.FindItemByName(projectItems, Path.GetFileName(SourceFilePath), false);
                if (outputProjectItem == null)
                {
                    outputProjectItem = projectItems.AddFromFileCopy(SourceFilePath);
                }
            }
            else
            {
                outputProjectItem = projectItems.AddFromFile(SourceFilePath);
            }
        }

        #endregion
    }
}
